{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Choice of Nodes for Polynomial Interpolation"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib qt5"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import numpy.linalg as la\n",
        "\n",
        "from matplotlib.pyplot import (\n",
        "    clf, plot, show, xlim, ylim,\n",
        "    get_current_fig_manager, gca, draw, connect)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Choose a function below:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "func = \"sin\"\n",
        "\n",
        "if func == \"sin\":\n",
        "    def f(x):\n",
        "        return np.sin(5*x)\n",
        "elif func == \"jump\":\n",
        "    def f(x):\n",
        "        result = 0*x\n",
        "        result.fill(-1)\n",
        "        result[x > 0] = 1\n",
        "        return result\n",
        "elif func == \"runge\":\n",
        "    def f(x):\n",
        "        return 1/(1+25*x**2)\n",
        "else:\n",
        "    raise RuntimeError(\"unknown function '%s'\" % func)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Run this cell to play with the node placement toy:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYcAAAD8CAYAAACcjGjIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzt3XecVOW9x/HPb5elg1RpUgVEQARdQbCAHRsYRQXFWIN6xZa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v6HM8L5Oy3j8Nf3AS4EyjoQ4N4aEdl+WfYTcz0y8gdw13X9M0je8CGl5jSz2rsuw5hZPeAI4Jt0O56Rn/Vw/DXU13d7LZnHs9j32x7y9wPGRo7fCKC/+dZMLYE2+FEBEq3UjGbWBT/PSh/n3LKo5cX+/JOQsaw5G0U97QPMjjweDZwYyVsbP+xO9Nl4SnNGsh6Av6H7edSyVB7P0owAfh1ptXQ4sDbyQap8xzIVd9n3cPf9V/iqtgVYCoyOLG8MjNrtLvy/8dX4lqjlrfC/fEXA60ClJOWsC3wIzI18rRNZXgg8GbVeC+BHIG+37x8LzMD/EfsnUD1UTqBHJMu0yNdL0/F4AgOBbcDUqH+dU3E8i3u/4S9b9Yk8rhw5PkWR49Uq6ntviXzfHODkJP7ulJZxTOR3atexG1Hazz9QzruAWZE844B2Ud97SeQYFwEXh8wZef5n4O7dvi9lxxP/oXNJ5PdiEf5e0hXAFZHXDXgs8n+YQVQL0PIcSw2fISIiMTLhspKIiKSYioOIiMRQcRARkRgqDiIiEkPFQUREYqg4iIhIDBUHERGJ8X9Tfirkwc9MTgAAAABJRU5ErkJggg==\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "needs_background": "light"
          },
          "output_type": "display_data"
        }
      ],
      "source": [
        "#keep\n",
        "x_points = []\n",
        "y_points = []\n",
        "deg = [1]\n",
        "\n",
        "def update_plot():\n",
        "    clf()\n",
        "    xlim([-1, 1])\n",
        "    ylim([-1.5, 1.5])\n",
        "    gca().set_autoscale_on(False)\n",
        "    plot(x_points, y_points, 'o')\n",
        "\n",
        "    x = np.linspace(-1, 1, 500)\n",
        "    plot(x, f(x), \"--\")\n",
        "\n",
        "    if len(x_points) >= deg[0]+1:\n",
        "        eval_points = np.linspace(-1, 1, 500)\n",
        "        poly = np.poly1d(np.polyfit(\n",
        "            np.array(x_points),\n",
        "            np.array(y_points), deg[0]))\n",
        "        plot(eval_points, poly(eval_points), \"-\")\n",
        "\n",
        "\n",
        "def click(event):\n",
        "    \"\"\"If the left mouse button is pressed: draw a little square. \"\"\"\n",
        "    tb = get_current_fig_manager().toolbar\n",
        "    if event.button == 1 and event.inaxes and tb.mode == '':\n",
        "        x_points.append(event.xdata)\n",
        "        x_ary = np.array([event.xdata])\n",
        "        y_ary = f(x_ary)\n",
        "        y_points.append(y_ary[0])\n",
        "\n",
        "    if event.button == 3 and event.inaxes and tb.mode == '':\n",
        "        if len(x_points) >= deg[0]+2:\n",
        "            deg[0] += 1\n",
        "\n",
        "    update_plot()\n",
        "    draw()\n",
        "\n",
        "update_plot()\n",
        "connect('button_press_event', click)\n",
        "show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.3rc1"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}